//
//  SelecterToolsViewController.m
//  SelecterTools
//
//  Created by ZX on 16-4-11.
//  Copyright (c) 2016年 CRQ. All rights reserved.
//

#import "SelecterToolsViewController.h"
#import "SelecterToolsScrolView.h"
#import "SelecterContentScrollView.h"
#import "publicViewController.h"
#import "shoppingCarViewController.h"
#import "logRegisterViewController.h"


#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height


@interface SelecterToolsViewController ()
<UISearchBarDelegate,UITabBarControllerDelegate>
{
    UIView * _view;
    NSMutableArray * imageArray;
    UIImageView * flashView;
    UIButton * shoppingCarBtn;
}


@property(nonatomic,assign)BOOL isFirst;

@property(nonatomic,retain)NSArray *titleArr;
@property(nonatomic,retain)NSMutableArray *vcArr;

@property(nonatomic,retain)SelecterToolsScrolView *selectTools;
@property(nonatomic,retain)SelecterContentScrollView *contentScrView;

@end

@implementation SelecterToolsViewController

-(void)viewWillAppear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tabBarController.delegate = self;
    
    _titleArr = @[@"分类",@"品牌",@"首页",@"专题",@"礼物"];
    
    _vcArr = [NSMutableArray array];
    
    for (int i = 0; i < 5 ; i++) {
        publicViewController *vc = [[publicViewController alloc]init];
        [self addChildViewController:vc];
        [_vcArr addObject:vc];
        vc.index = i;
    }
    [self createSelecterToolsSrc];
    [self createContentVCSrc];
    [self updateVCViewFrom:2];
    
    self.edgesForExtendedLayout=UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars=NO;
    self.modalPresentationCapturesStatusBarAppearance=NO;
    //    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"商店";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bar"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:15.0f]}];
    
    
    _view = [[UIView alloc]initWithFrame:self.view.frame];
    _view.backgroundColor = [UIColor clearColor];
    UIWindow * window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:_view];
    
    imageArray = [[NSMutableArray alloc]initWithCapacity:0];
    flashView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    flashView.image = [UIImage imageNamed:@"bg2"];
    [_view addSubview:flashView];
    for (int i = 0;  i < 35; i++)
    {
        NSString * imageStr = [NSString stringWithFormat:@"splash_5_%d",i+2];
        UIImage * image = [UIImage imageNamed:imageStr];
        [imageArray addObject:image];
        
    }
    [self performSelector:@selector(animation) withObject:nil afterDelay:2];
    [self performSelector:@selector(yanChi) withObject:nil afterDelay:5.0];
    
    
    UIButton * searchBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [searchBtn setBackgroundImage:[UIImage imageNamed:@"btn_nav_search"] forState:UIControlStateNormal];
    searchBtn.frame = CGRectMake(0, 0, 20, 20);
    [searchBtn addTarget:self action:@selector(searchClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * searchBar = [[UIBarButtonItem alloc]initWithCustomView:searchBtn];
    self.navigationItem.leftBarButtonItem = searchBar;
    
    shoppingCarBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [shoppingCarBtn setBackgroundImage:[UIImage imageNamed:@"btn_cart"] forState:UIControlStateNormal];
    shoppingCarBtn.frame = CGRectMake(0, 0, 40, 40);
    [shoppingCarBtn addTarget:self action:@selector(carBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * shoppingCarBar = [[UIBarButtonItem alloc]initWithCustomView:shoppingCarBtn];
    UIBarButtonItem * negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -5;
    
    self.navigationItem.rightBarButtonItem = shoppingCarBar;
    
    
    
    //    UIView * mainView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //    mainView.backgroundColor = [UIColor yellowColor];
    
    
    
}
-(void)animation
{
    flashView.animationImages = imageArray;
    flashView.animationDuration = 3;
    flashView.animationRepeatCount = 1;
    [flashView startAnimating];
}
-(void)yanChi
{
    [_view removeFromSuperview];
}

-(void)searchClick:(UIButton *)sender
{
    UIView * titView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, 44)];
    //    titView.backgroundColor = [UIColor blackColor];
    
    UISearchBar * searcBar = [[UISearchBar alloc]init];
    searcBar.frame = CGRectMake(-30.0f,5.0f,self.view.frame.size.width-120,30.0f);
    [searcBar setPlaceholder:@"|搜索你喜欢的"];
    [searcBar setSearchBarStyle:UISearchBarStyleMinimal];
    [searcBar setBackgroundColor:[UIColor blackColor]];
    UITextField *searchField = [searcBar valueForKey:@"_searchField"];
    
    // Change search bar text color
    searchField.textColor = [UIColor whiteColor];
    //    [searcBar setShowsCancelButton:YES];
    searcBar.delegate = self;
    [titView addSubview:searcBar];
    
    UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(searcBar.frame.size.width+20, 12, 40, 20);
    cancelBtn.backgroundColor = [UIColor blackColor];
    [cancelBtn setTitle:[NSString stringWithFormat:@"取消"] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    UIBarButtonItem * cancelBtnBar = [[UIBarButtonItem alloc]initWithCustomView:cancelBtn];
    self.navigationItem.rightBarButtonItem = cancelBtnBar;
    
    //    cancelBtn.userInteractionEnabled = YES;
    //    [titView addSubview:cancelBtn];
    
    self.navigationItem.titleView = titView;
    [searcBar becomeFirstResponder];
    //    shoppingCarBtn.enabled = NO;
}

-(void)carBtnClick:(UIButton *)sender
{
//    NSLog(@"---------");
//    shoppingCarViewController * vc = [[shoppingCarViewController alloc]init];
//    
//    [self.navigationController pushViewController:vc animated:YES];

    logRegisterViewController * logVC = [[logRegisterViewController alloc]init];
    [self presentViewController:logVC animated:YES completion:nil];
}


-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    
}

-(void)cancelBtnClick
{
//    NSLog(@"*******");
    self.navigationItem.titleView = nil;
    UIBarButtonItem * shoppingCarBar = [[UIBarButtonItem alloc]initWithCustomView:shoppingCarBtn];
    self.navigationItem.rightBarButtonItem =shoppingCarBar;
    
    
}




-(void)createSelecterToolsSrc
{
    __weak typeof(self) weakSelf = self;
    
    _selectTools = [[SelecterToolsScrolView alloc]initWithSeleterConditionTitleArr:_titleArr andBtnBlock:^(UIButton * btn) {
        [weakSelf updateVCViewFrom:btn.tag];
    }];
    [self.view addSubview:_selectTools];
}

-(void)createContentVCSrc
{
    __weak typeof(self) weakSelf = self;
    _contentScrView = [[SelecterContentScrollView alloc]initWithSeleterConditionTitleArr:_vcArr andBtnBlock:^(int index) {
        [weakSelf updateSelectToolsIndex:index];
    }];
    [self.view addSubview:_contentScrView];
}


-(void)updateSelectToolsIndex:(NSInteger)index
{
    [_selectTools updateSelecterToolsIndex:index];
}

-(void)updateVCViewFrom:(NSInteger )index
{
    [_contentScrView updateVCViewFromIndex:index];
}


-(void)dealloc
{
    NSLog(@"dealloc");
}

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    UINavigationController * vc = (UINavigationController *)viewController;
    
     if ([vc.topViewController isKindOfClass:[self class]]) {
         if (_isFirst)
         {
             _isFirst = NO;
             return YES;
         }else
         {
             return NO;
         }
         
    }else
    {
        _isFirst = YES;
        return YES;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
